package com.bwie.controller;

import com.bwie.bean.Canteen;
import com.bwie.bean.City;
import com.bwie.bean.Food;
import com.bwie.service.CanteenService;
import com.bwie.util.FileUploadUtil;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class CanteenController {

    @Autowired
    CanteenService canteenService;


    @RequestMapping("list.do")
    public String list(
            @RequestParam(value = "pageNum",defaultValue = "1") Integer pageNum,
            Model model,
            String cname,
            String opentimeX,
            String opentimeD,
            String sheng,
            String shi,
            String xian
    ){
        Map map = new HashMap();
        map.put("cname",cname);
        map.put("opentimeX",opentimeX);
        map.put("opentimeD",opentimeD);
        map.put("sheng",sheng);
        map.put("shi",shi);
        map.put("xian",xian);

        PageHelper.startPage(pageNum,3);
        List<Canteen>list=canteenService.list(map);
        PageInfo pageInfo = new PageInfo(list);
        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("map",map);
        return "list";
    }
    //动态获取食物
    @RequestMapping("listFood.do")
    @ResponseBody
    public List<Food>listFood(){
        List<Food>listFood=canteenService.listFood();
        return listFood;
    }

    //动态获取三级联动
    @RequestMapping("listCity.do")
    @ResponseBody
    public List<City>listCity(Integer pid){
        List<City>listCity=canteenService.listCity(pid);
        return listCity;
    }
    //添加
    @RequestMapping("toadd.do")
    public String toadd(){
        return "add";
    }
    @RequestMapping("add.do")
    @ResponseBody
    public Object add(Canteen canteen, @RequestParam("photo") MultipartFile photo, HttpSession session,Integer[]fids){
        if (photo!=null){
            String upload = FileUploadUtil.load(session, photo, "upload");
            canteen.setPic(upload);
        }
        int a=canteenService.add(canteen,fids);
        return a;
    }
    //toupdate修改
    @RequestMapping("toupdate.do")
    public String toupdate(Integer cid){
        return "update";
    }
    @RequestMapping("update.do")
    @ResponseBody
    public Object update(Canteen canteen, @RequestParam("photo") MultipartFile photo, HttpSession session,Integer[]fids){
        if (photo!=null){
            String upload = FileUploadUtil.load(session, photo, "upload");
            canteen.setPic(upload);
        }
        int a=canteenService.update(canteen,fids);
        return a;
    }
    //单删除
    @RequestMapping("del.do")
    @ResponseBody
    public Object del(Integer cid){
        int a=canteenService.del(cid);
        return a;
    }
    //批量删除
    @RequestMapping("dels.do")
    @ResponseBody
    public Object del(String cid){
        int a=canteenService.dels(cid);
        return a;
    }
    //h回显
    @RequestMapping("listHui.do")
    @ResponseBody
    public Canteen listHui(Integer cid){
        Canteen listHui=canteenService.listHui(cid);
        return listHui;
    }

}
